﻿using System;
using System.Data.SqlClient;
using System.Web;
using System.Web.Configuration;

public partial class Katalog_Drzwi : BasePage
{
    private SqlConnection _connection;
    private SqlCommand _command;
    private SqlDataReader _reader;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        /*
         * Synchronous operation
         *
        string connectionString = WebConfigurationManager.ConnectionStrings["KursyConnectionString"].ConnectionString;
        string commandString = "Select * from KursyA";
        SqlConnection conn = new SqlConnection(connectionString);
        SqlCommand command = new SqlCommand(commandString);
        conn.Open();
        command.Connection = conn;
        SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
        Output.DataSource = reader;
        Output.DataBind();
        /**/
        /*
         * Asynchronous operation
         */
        if (!IsPostBack)
        {
            // Hook PreRenderComplete event for data binding
            // this.PreRenderComplete += new EventHandler(Page_PreRenderComplete);
            // Register async methods
            AddOnPreRenderCompleteAsync(
                new BeginEventHandler(BeginAsyncOperation),
                new EndEventHandler(EndAsyncOperation)
            );
        }
        /**/
    }
    IAsyncResult BeginAsyncOperation (object sender, EventArgs e, 
        AsyncCallback cb, object state)
    {
        Trace.Warn("BeginHandler");

        string connectionString = WebConfigurationManager.ConnectionStrings["KursyConnectionString"].ConnectionString;
        _connection = new SqlConnection(connectionString);
        _connection.Open();
        _command = new SqlCommand("Select top 10 * from KursyA", _connection);
        return _command.BeginExecuteReader(cb, state);
    }
    void EndAsyncOperation(IAsyncResult ar)
    {
        Trace.Warn("EndHandler");

        _reader = _command.EndExecuteReader(ar);
    }
    protected void Page_PreRenderComplete(object sender, EventArgs e)
    {
        ExchangeRates.DataSource = _reader;
        ExchangeRates.DataBind();
    }
    public override void Dispose()
    {
        if (_connection != null) _connection.Close();
        base.Dispose();
    }
}